package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.response.JsonResuli;
import cn.tedu._05mvcboot02.dto.AddUserDTO;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "01.用户模块")
@Slf4j
@RestController
@RequestMapping(value = "/v1/users/")
/*
 RestController注解:
1.组合注解:相当于 Controller + ResponseBody;不
圣
2.在控制器类上添加此注解后,所有的控制器方法上都无需再添加 @ResponseBody 注解了
 */
public class UserController {
    /**
     * 添加用户功能
     */
    @Autowired
    private UserMapper userMapper;
//    @RequestMapping(value = "insert")
//    @ResponseBody
@ApiOperation("注册功能")
    @PostMapping(value = "insert")
    public JsonResuli addUser(AddUserDTO addUserDTO){
        User user = new User();
        BeanUtils.copyProperties(addUserDTO, user);
        user.setCreated(new Date());

        userMapper.insertUser(user);
        return JsonResuli.ok();
    }


    /**
     * 查询用户列表
     */
//    @RequestMapping(value = "userlist")
//    @ResponseBody
    @ApiOperation("查询用户列表")
    @GetMapping(value ="userlist" )
    public JsonResuli userList(){
        System.out.println(userMapper.selectUSerList());
        return JsonResuli.ok(userMapper.selectUSerList());
    }

    /**
     * 删除指定用户
     */
//    @RequestMapping(value = "delete")
//    @ResponseBody
    @ApiImplicitParam(name = "id",value = "用户id",
                      required = true,dataType = "int" )
    @ApiOperation("删除指定用户")
    @GetMapping(value = "delete")
    public JsonResuli deleteUser(int id){
        if (id<0){
            throw new IllegalArgumentException("ID值不能小于0");
        }



        userMapper.deleteUser(id);

        return JsonResuli.ok();
    }

    /**
     * 更新指定用户
     */
//    @RequestMapping(value = "update")
//    @ResponseBody
    @ApiOperation("更新指定用户")
    @PostMapping(value = "update")
    public String updateUser(User user){
        userMapper.updateUser(user);
        return "修改成功";
    }

    @ApiImplicitParam(name = "id",value = "用户id",
            required = true,dataType = "int" )
    @ApiOperation("查询指定用户")
    @GetMapping(value = "{id}")
    public JsonResuli getUserById(@PathVariable(value = "id") int id){
        return JsonResuli .ok(userMapper.selectUSerById(id));
    }
}
